/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "tracplugin.h"

#include "tracimportwizard.h"
//#include "tracexportwizard.h"
#include "ticketsviewnavigationfactory.h"
#include "projectmanagementpanel.h"

#include <projectexplorer/session.h>

#include <coreplugin/mainwindow.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/modemanager.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/filemanager.h>

#include <utils/parameteraction.h>
#include <locator/commandlocator.h>

#include <coreplugin/coreconstants.h>
#include <coreplugin/dialogs/iwizard.h>

#include <QtCore/QDebug>
#include <QtCore/QtPlugin>
#include <QtGui/QAction>
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>
#include <QtGui/QPushButton>
#include "../tasktop4qtcreator/tasklistcontrollerregistry.h"
#include "tractasklistcontroller.h"

#include "traccreatequerydialog.h"
#include "../tasktop4qtcreator/tasklistutil.h"

#include <QxtCore/QxtLogger>
#include <QxtCore/QxtBasicFileLoggerEngine>

using namespace Trac::Internal;
using namespace ProjectExplorer;

TracPlugin::TracPlugin()
    : m_core(0)
{
}

/*! Plugins are responsible for deleting objects they created on the heap, and
    to unregister objects from the plugin manager that they registered there.
*/
TracPlugin::~TracPlugin()
{
}

/*! Initializes the plugin. Returns true on success.
    Plugins want to register objects with the plugin manager here.

*/
bool TracPlugin::initialize(const QStringList &arguments, QString *error_message)
{
    Q_UNUSED(arguments);
    Q_UNUSED(error_message);
    m_core = Core::ICore::instance();
    Core::ActionManager *actionManager = m_core->actionManager();

    // Create the globalcontext list to register actions accordingly
    Core::Context globalcontext(Core::Constants::C_GLOBAL);

    Core::ActionContainer *toolsContainer = actionManager->actionContainer(Core::Constants::M_TOOLS);
    Core::ActionContainer *cpContainer    = actionManager->createMenu(QString("Project hosting"));
    cpContainer->menu()->setTitle(tr("&Project hosting"));
    toolsContainer->addMenu(cpContainer);
    Core::Command *command;

    m_importAction = new QAction(QIcon(Core::Constants::ICON_OPENFILE), tr("Import project"), this);
    command = actionManager->registerAction(m_importAction, "trac.import", globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+I")));
    connect(m_importAction, SIGNAL(triggered()), this, SLOT(importAction()));
    cpContainer->addAction(command);

//    m_exportAction = new QAction(tr("Export project"), this);
//    command = actionManager->registerAction(m_exportAction, "trac.export", globalcontext);
//    command->setDefaultKeySequence(QKeySequence(tr("Alt+E")));
//    connect(m_exportAction, SIGNAL(triggered()), this, SLOT(exportAction()));
//    cpContainer->addAction(command);

    // Create wizard page
    addAutoReleasedObject(new TracImportWizard());

    m_session = new SessionManager(this);

    addAutoReleasedObject(new TicketsViewNavigationFactory);
    addAutoReleasedObject(new ProjectManagementPanelFactory(m_session));

    //register TracTaskListController with tasktop4qtcreator plugin
    //if (!TaskListUtil::isTasktopAvailable())
    //{
    QPointer<TracTaskListController> tracTaskListController = new TracTaskListController();
    TaskListControllerRegistry::setTaskListController(tracTaskListController);
    //}

    /*QxtBasicFileLoggerEngine *engine = new QxtBasicFileLoggerEngine(QDir::home().absolutePath().append("/").append("tracplugin.log"));
    engine->setDateFormat("yyyyMMdd hh:mm:ss.zzz");
    qxtLog->addLoggerEngine("tracplugin", engine);
    qxtLog->initLoggerEngine("tracplugin");
    qxtLog->enableLoggerEngine("tracplugin");
    qxtLog->enableAllLogLevels("tracplugin");*/
    qxtLog->disableAllLogLevels();
    qxtLog->debug(QString("Initalized Trac plugin"));

    return true;
}

void TracPlugin::importAction()
{
    QList <Core::IWizard *> hosted;
    TracImportWizard *wizard = new TracImportWizard();
    hosted << wizard;
    Core::ICore::instance()->showNewItemDialog("", hosted);
    delete wizard;
}

void TracPlugin::exportAction()
{
    /*QList <Core::IWizard *> hosted;
    TracExportWizard *wizard = new TracExportWizard();
    hosted << wizard;
    Core::ICore::instance()->showNewItemDialog("", hosted);
    delete wizard;*/
}

void TracPlugin::extensionsInitialized()
{
}

Q_EXPORT_PLUGIN(TracPlugin)
